﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Admin;
using Common;
using Common.Controls;
using System.IO;

namespace SLAdmin.UserControls
{
    public partial class EditSurvey : UserControl
    {
        Survey _survey;
        Guid _surveyId;
        App _myApp = App.GetApplication;
        StudyManager _studyManager = StudyManager.GetInstance();
        StudyDataClient _studyDataClient = new StudyDataClient();
        DialogHelper _dialogHelper = new DialogHelper();

        public EditSurvey()
        {
            InitializeComponent();
        }
        public EditSurvey(Survey Survey)
        {
            InitializeComponent();
            _survey = Survey;
            _surveyId = Survey.SurveyId;
            this.DataContext = _survey;
            SurveyPageGrid.ItemsSource = _studyDataClient.GetSurveyPages(_surveyId);
            _studyDataClient.CreateSurveyPageCompleted += new StudyDataClient.CreateSurveyPageCompletedDelegate(_myApp_CreateSurveyPageCompleted);
            _studyDataClient.DeleteSurveyPageCompleted += new StudyDataClient.DeleteSurveyPageCompletedDelegate(_myApp_DeleteSurveyPageCompleted);
            _dialogHelper.DialogAccepted += new DialogHelper.DialogAcceptedDelegate(_myApp_DialogAccepted);
        }

        void _myApp_DialogAccepted(object DataContext)
        {
            if (DataContext is SurveyPage)
            {
                _studyDataClient.DeleteSurveyPage(DataContext as SurveyPage);            
            }
        }

        void _myApp_DeleteSurveyPageCompleted(SurveyPage PageToDelete)
        {
            SurveyPageGrid.SelectedItem = null; 
            List<SurveyPage> list = SurveyPageGrid.ItemsSource as List<SurveyPage>;
            list.Remove(PageToDelete);
            SurveyPageGrid.ItemsSource = null;
            SurveyPageGrid.ItemsSource = list;            
            SurveyUpdated();
        }

        void _myApp_CreateSurveyPageCompleted(SurveyPage newPage)
        {
            SurveyPageGrid.SelectedItem = null;            
            List<SurveyPage> list = SurveyPageGrid.ItemsSource as List<SurveyPage>;
            list.Add(newPage);
            SurveyPageGrid.ItemsSource = null;
            SurveyPageGrid.ItemsSource = list;
            SurveyUpdated();
        }
        public delegate void SurveyUpdatedDelegate();
        public event SurveyUpdatedDelegate SurveyUpdated;
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            _studyDataClient.UpdateSurvey(_survey);
            _studyDataClient.UpdateSurveyPages(SurveyPageGrid.ItemsSource as List<SurveyPage>, _surveyId);
            foreach (SurveyPage sp in SurveyPageGrid.ItemsSource as List<SurveyPage>)
            {
                _studyDataClient.UpdateSurveyPage(sp);
            }
            SurveyUpdated();
        }

        private void btnCreatePage_Click(object sender, RoutedEventArgs e)
        {
            _studyDataClient.CreateSurveyPage(_surveyId);
        }

        private void Up_Click(object sender, RoutedEventArgs e)
        {
            List<SurveyPage> list = SurveyPageGrid.ItemsSource as List<SurveyPage>;            
            Button MyButton = (Button)sender;
            SurveyPage mySurveyPage = (SurveyPage)MyButton.DataContext;
            if (list.IndexOf(mySurveyPage) != 0)
            {
                list.Reverse(list.IndexOf(mySurveyPage) - 1, 2);
                SurveyPageGrid.ItemsSource = null;
                SurveyPageGrid.ItemsSource = list;                
            }
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {            
            List<SurveyPage> list = SurveyPageGrid.ItemsSource as List<SurveyPage>;
            Button MyButton = (Button)sender;
            SurveyPage mySurveyPage = (SurveyPage)MyButton.DataContext;
            if (list.IndexOf(mySurveyPage) != list.Count - 1)
            {
                list.Reverse(list.IndexOf(mySurveyPage), 2);
                SurveyPageGrid.ItemsSource = null;
                SurveyPageGrid.ItemsSource = list;                
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            SurveyPage PageToDelete = (sender as Button).DataContext as SurveyPage;
            _dialogHelper.Alert("Confirm delete", "Are you sure you want to delete this survey page?", PageToDelete,LayoutRoot);                                 
        }

        private void btnDeleteResource_Click(object sender, RoutedEventArgs e)
        {
            string Path = (sender as Button).DataContext as string;
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.DeleteResourceCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(delegate(object sender1, System.ComponentModel.AsyncCompletedEventArgs e1)
            {
                BindResources();
            });
            a.DeleteResourceAsync(Path, _surveyId);
        }

        private void BindResources()
        {
            AdminServiceWCFClient a = StudyManager.GetAdminServiceWCFClient();
            a.GetResourcesForStudyIdCompleted += new EventHandler<GetResourcesForStudyIdCompletedEventArgs>(delegate(object sender1, GetResourcesForStudyIdCompletedEventArgs e1)
            {
                ResourcesGrid.ItemsSource = e1.Result;
            });
            a.GetResourcesForStudyIdAsync(_surveyId);
        }

        private void StudyTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems[0].Equals(tabResources))
            {
                BindResources();
            }
        }

        private void btnUploadFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.Filter = "All files|*.*";
            if (ofd.ShowDialog().Value)
            {
                UploadFileHandler Uploader = new UploadFileHandler();
                foreach (FileInfo File in ofd.Files)
                {
                    Uploader.UploadFile(_surveyId, File.Name, File.OpenRead());
                }
            }
        }

        private void UserControl_LayoutUpdated(object sender, EventArgs e)
        {
            StudyTabs.Width = this.ActualWidth;
        }
    }
}
